﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using olierFrm.DataBase.FrameWork;
using olierFrm.WebFormDev.Session;
using System.Configuration;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using System.Data;
using olierFrm.Extensions.EDataTable;

namespace olierFrm.WebFormDev
{
    public static class Extensions
    {
        /// <summary>
        /// 增加系统参数，Operator
        /// </summary>
        /// <param name="sqs"></param>
        public static void AddSystemParams(this DefinedSQL sqs)
        {
            try
            {
                sqs.SetParam("Operator", Session.SSession.User.Operator);
            }
            catch { }
            try
            {

                sqs.SetParam("StoreNO", Session.SSession.User.StoreNO);
            }
            catch { }
        }

        public static void BindQueryParamToStdQuery(this DefinedSQL sqs)
        {
            foreach (var k in HttpContext.Current.Request.QueryString.AllKeys)
            {
                var osp = sqs.FindParamByName(k);
                if (osp != null)
                    osp.ParamValue = HttpContext.Current.Request.QueryString[k];
            }
        }


        public static void ASPxDateEditStyle(this ASPxDateEdit control)
        {
            control.DisplayFormatString = "yyyy-MM-dd";
            control.EditFormatString = "yyyy-MM-dd";
            control.CalendarProperties.MonthGridPaddings.Padding = 0;
            control.CalendarProperties.HeaderStyle.Paddings.Padding = 0;
            control.CalendarProperties.TodayButtonText = "今天";
            control.CalendarProperties.ClearButtonText = "清空";
            control.CalendarProperties.DayOtherMonthStyle.Paddings.Padding = 0;
            control.CalendarProperties.DayStyle.Paddings.Padding = 0;
            control.CalendarProperties.FooterStyle.Paddings.PaddingTop = 2;
            control.CalendarProperties.FooterStyle.Paddings.PaddingBottom = 2;
            control.CalendarProperties.ShowWeekNumbers = false;
        }
        /// <summary>
        /// 自动宽度，必须在数据绑定后才能调用。
        /// </summary>
        /// <param name="grid"></param>
        /*public static void AutoWidth(this ASPxGridView grid)
        {
            //return;
            var dtSource = (DataTable)grid.DataSource;
            for (int i = 0; i < 5 && i < dtSource.Rows.Count; i++)
            {
                for (int j = 0; j < grid.Columns.Count; j++)
                {
                    if (dtSource.Columns[j].ExtendedProperties.ContainsKey("PropCommonWidth"))
                    {
                        grid.Columns[j].Width = Convert.ToInt32(dtSource.Columns[j].ExtendedProperties["PropCommonWidth"]);
                        continue;
                    }
                    int intTemp = Encoding.Default.GetBytes(dtSource.Rows[i][j].ToString()).Length * 7;
                    int intTempHeader = Encoding.Default.GetBytes(grid.Columns[j].ToString()).Length * 9;

                    if (grid.Columns[j].Width.Value < intTemp && intTempHeader < intTemp)
                    {
                        grid.Columns[j].Width = intTemp;
                    }

                }
            }
        }*/

        #region ASPxGridView属性
        public static void AddUpdateFlagColumn(this ASPxGridView grid)
        {
            grid.Columns.Insert(0, new GridViewDataColumn() { Caption = "删除", FieldName = "UpdateFlag" });
        }


        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="grid"></param>
        ///// <param name="columnsName"></param>
        //public static void HideColumn(this ASPxGridView grid, List<string> columnsName)
        //{
        //    columnsName.Remove("");
        //    columnsName.Add("Color");
        //    foreach (var h in columnsName)
        //    {
        //        if (grid.Columns[h] == null) continue;
        //        try
        //        {
        //            if (h != null)
        //                grid.Columns[h].Visible = false;
        //        }
        //        catch
        //        {
        //            throw new Exception("在处理" + grid.ID + "隐藏字段时，没有找到字段：" + h);
        //        }
        //    }
        //}

        public static void AddHideColumn(this ASPxGridView grid, List<string> columnsName)
        {

        }



        /// <summary>
        /// 绑定分组
        /// </summary>
        /// <param name="grid"></param>
        /// <returns></returns>
        public static ASPxGridView BindGroupToGrid(this ASPxGridView grid)
        {
            var group = SGrid.GetGroup(grid.GetQueryName());
            if (group != null)
            {
                foreach (var g in group)
                {
                    grid.GroupBy(grid.Columns[g]);
                }
            }
            return grid;
        }


        public static ASPxGridView HideField(this ASPxGridView grid)
        {
            var dtSource = (DataTable)grid.DataSource;
            var hfs = dtSource.GetHideColumn();

            //var hfs = dbStdQuery.StdQuery.HideFields.Split(';').ToList();
            hfs.Add(dtSource.GetDataRowIndexFieldName());
            hfs.Add("Color");
            foreach (var h in hfs)
            {
                if (grid.Columns[h] != null)
                    grid.Columns[h].Visible = false;
            }
            return grid;
        }

       

        /// <summary>
        /// 设置ASPxGridView的展开行序号
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static ASPxGridView SetExpandRow(this ASPxGridView grid, int index)
        {
            grid.Attributes.Add("ExpandRowIndex", index.ToString());
            return grid;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="grid"></param>
        /// <returns></returns>
        public static int GetExpandRow(this ASPxGridView grid)
        {
            return Convert.ToInt32(grid.Attributes["ExpandRowIndex"]);
        }

        /// <summary>
        /// 设置ASPxGridView的Title
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="Title"></param>
        /// <returns></returns>
        public static ASPxGridView SetTitle(this ASPxGridView grid, string Title)
        {
            grid.Attributes.Add("Title", Title);
            return grid;
        }

        /// <summary>
        /// 获取ASPxGridView的Title
        /// </summary>
        /// <param name="grid"></param>
        /// <returns></returns>
        public static string GetTitle(this ASPxGridView grid)
        {
            return grid.Attributes["Title"];
        }


        /// <summary>
        /// 设置ASPxGridView的QueryName
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="QueryName"></param>
        /// <returns></returns>
        public static ASPxGridView SetQueryName(this ASPxGridView grid, string QueryName)
        {
            grid.Attributes.Add("QueryName", QueryName);
            return grid;
        }
        /// <summary>
        /// 获取ASPxGridView的QueryName
        /// </summary>
        /// <param name="grid"></param>
        /// <returns></returns>
        public static string GetQueryName(this ASPxGridView grid)
        {
            return grid.Attributes["QueryName"];
        }

       


        public static void FormatDisplayString(this ASPxGridView grid, string type, string displayFormat)
        {

            for (int i = 0; i < grid.Columns.Count; i++)
            {
                if (grid.Columns[i].GetType().Name.ToUpper() == type.ToUpper())//"GridViewDataDateColumn"
                {
                    switch (type.ToUpper())
                    {
                        case "GRIDVIEWDATADATECOLUMN":
                            GridViewDataDateColumn gvddc = (GridViewDataDateColumn)grid.Columns[i];
                            gvddc.PropertiesDateEdit.DisplayFormatString = displayFormat;// "yyyy-MM-dd";
                            break;
                    }

                }
            }

        }

        #endregion

        public static string AppTitle
        {
            get
            {
                return ConfigurationManager.AppSettings["AppTitle"];
            }

        }
        public static string AppUrl
        {
            get
            {
                return ConfigurationManager.AppSettings["AppUrl"];
            }

        }
    }
}
